var order = {
	init : function() {
		order.orderDataGrid = $('#orderDataGrid');
		order.orderProductPropertyGrid = $('#orderProductPropertyGrid');
		order.orderProductsListDialog = $('#orderProductsListDialog');
		
		order.orderDataGrid.datagrid({
		    url: window.ctx + '/sys/order/listAllStoreOrder',
		    //fitColumns: true,
		    striped: true,
		    rownumbers: true,
		    pagination: true,
		    pageSize: 10,
		    pageList: [5, 8, 10, 15],
		    pageNumber: 1,
		    columns:[[
				{field:'id', width:100, checkbox: true},
				{field:'sysUserId', title:'用户ID', width:80, align:'center'},
				{field:'receiver', title:'收件人', width:80, align:'center'},
				{field:'storeOrderProductId', title:'订单商品', width:100, align:'center',formatter: order.rows.productFormatter},
				{field:'phone', title:'联系电话', width:100, align:'center'},
				{field:'receiveAddress', title:'收件地址', width:500, align:'center'},
				{field:'msg', title:'买家留言', width:300, align:'center'},
				{field:'storeTicket', title:'发票', width:150, align:'left',formatter: order.rows.ticketFormatter},
				{field:'payState', title:'状态', width:80, align:'center',formatter: order.rows.payStateFormatter},
				{field:'state', title:'操作', width:80, align:'center',formatter: order.rows.operationFormatter},
		    ]],
		});
	},
	
	rows:{
		storeProductsIdsStr : [],
		storeOrderIds : [],
		productFormatter: function(value,row,index){
			if(value==null){
				return "无";
			}else{
				order.rows.storeProductsIdsStr[index] = value;
				return '<a href="#" onclick="order.rows.getOrderProducts(' + index + ')">查看商品</a>';
			}
		},
		ticketFormatter: function(value,row,index){
			if(value==null){
				return "不需要";
			}else{
				return "<p>种类：" + value.typeStr + "</p><p>类型：" + value.titleTypeStr + "</p><p>抬头：" + value.title + "</p>";
			}
		},
		
		payStateFormatter: function(value,row,index){
			if(value == 1){
				return "待发货";
			}else if(value == 2){
				return "已发货";
			}else{
				return "已完成";
			}
		},
		operationFormatter: function(value,row,index){
			order.rows.storeOrderIds[index] = row.id;
			if(row.payState == 1){
				return '<a href="#" onclick="order.rows.sendProducts(' + index + ')" >发货</a>';
			}else if(row.payState == 2){
				return "<a style='color:#999999'>配送中</a>";
			}else{
				return '<a href="#" onclick="order.rows.deleteOrder(' + index + ')" >删除</a>';
			}

		},
		
		getOrderProducts: function(index){
			order.orderProductPropertyGrid.propertygrid({
				url : window.ctx + '/sys/order/getOrderProducts?storeProductsIdsStr='+order.rows.storeProductsIdsStr[index],
				columns:[[
					{field:'storeProductId', title:'商品ID', width:300, align:'center'},
					{field:'name', title:'商品名称', width:150, align:'center'},
					{field:'count', title:'数量', width:60, align:'center'},
					{field:'subtotal', title:'小计', width:80, align:'center'}
				]]
			})
			order.orderProductsListDialog.dialog('open');
		},
		sendProducts: function(index){
			$.post(window.ctx +'/sys/order/sendProducts?orderId='+order.rows.storeOrderIds[index],function(data){
				order.orderDataGrid.datagrid('reload');
			});
		},
		
		deleteOrder: function(index){
			$.post(window.ctx +'/sys/order/deleteOrder?orderId='+order.rows.storeOrderIds[index],function(data){
				order.orderDataGrid.datagrid('reload');
			});
		},
		
	}
};

$(function(){
	order.init();
});